// pages/institute/list/list.js
import {
  getCompanyList
} from '../../../api/institute.js'
import {
  getAllOptions
} from '../../../api/other'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 1,
    rowCount: 0,
    list: [],
    arrowUp: true,
    selectOptions: [],
    selectIndex: 0,
    name: '',
    classifyId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAllOptionsList();
    this.getData();

  },
  getAllOptionsList() {
    getAllOptions().then(res => {
      res.unshift({
        id: -1,
        name: '全部分类'
      })
      this.setData({
        selectOptions: res
      })
    })
  },
  navigatorInstitute(e) {
    if (getApp().globalData.userInfo.vip) {
      console.log(e.currentTarget)
      wx.navigateTo({
        url: '/pages/institute/details/details?id=' + e.currentTarget.dataset.id +'&isMine='+e.currentTarget.dataset.ismine,
      })
    } else {
      wx.showToast({
        title: '成为会员才可查看商学院详情',
        icon: 'none',
      })
    }
  },
  getData() {
    let classifyId = '';
    if (this.data.classifyId !== -1) {
      classifyId = this.data.classifyId
    }
    getCompanyList(this.data.name, classifyId, this.data.pageNo).then(res => {
      this.setData({
        list: this.data.list.concat(res.list),
        rowCount: res.rowCount
      })
    })
  },

  handleScrollBottom(e) {
    if (this.data.rowCount > this.data.list.length) {
      this.data.pageNo += 1;
      this.getData();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 选择框 - 选择
   * @param {object} e 
   */
  handleSelect(e) {
    this.setData({
      selectIndex: Number(e.currentTarget.dataset.index),
      classifyId: e.currentTarget.dataset.id,
      arrowUp: true
    })
    this.data.pageNo = 1;
    this.setData({
      list: []
    })
    this.getData();
  },
  /**
   * 选择框展示
   */
  handleArrowUp() {
    this.setData({
      arrowUp: !this.data.arrowUp
    })
  },
  /**
   * 输入公司名称
   */
  onInputName(e) {
    this.setData({
      name: e.detail.value,
    })
  },
  onSearch() {
    this.data.pageNo = 1;
    this.data.list.length = 0;
    this.getData();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})